//
//  UIViewController+Tracking.m
//  ECourier
//
//  Created by J  on 15/8/4.
//  Copyright (c) 2015年 CIMC. All rights reserved.
//

#import "UIViewController+Tracking.h"
#import <objc/runtime.h>
#import "TarckingDictionary.h"

@implementation UINavigationController (Tracking)

+ (void)load {

    NSLog(@"%s",__func__);

    // 交换实现viewWillAppear
    method_exchangeImplementations(class_getInstanceMethod(UINavigationController.class, @selector(pushViewController:animated:)),class_getInstanceMethod(self, @selector(trackingPushViewController:animated:)));
    
    //交换实现viewWillDisappear
    method_exchangeImplementations(class_getInstanceMethod(UINavigationController.class, @selector(popViewControllerAnimated:)), class_getInstanceMethod(self, @selector(trackingPopViewControllerAnimated:)));

    

}


- (UIViewController*)trackingPopViewControllerAnimated:(BOOL)animated{
    UIViewController* vc ;
    vc = [self trackingPopViewControllerAnimated:animated];//此处没有循环
    [self staticWithViewController:vc];

    return vc;
}

-(void)trackingPushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [self trackingPushViewController:viewController animated:animated];//此处没有循环
    [self staticWithViewController:viewController];
    NSLog(@"%@",self.class);

 
    
}


//统计事件
- (void)staticWithViewController:(UIViewController*)controller{
    NSString* str = NSStringFromClass(controller.class);
   NSString* value =  [TarckingDictionary getViewControllerIdentificationWithKey:str];
    if (value) {
        NSLog(@"value:%@",value);
    }

    
}




















#pragma mark - Method Swizzling
//
//- (void)tracking_viewDidLoad:(BOOL)animated {//页面出现
//    [self tracking_viewDidLoad:animated];
//    
//    
//    if ([self getControllerID]) {
//        //此处添加你想统计的打点事件
//        NSLog(@"%@现在进入%@",self.class,[self getControllerID]);
//    }
//    
//
//   
//    
//}
//
//
//
//- (void)tracking_dealloc{//页面销毁
//    
//    if ([self getControllerID]) {
//        //此处添加你想统计的打点事件
//        NSLog(@"页面%@退出销毁%@",self.class,[self getControllerID]);
//    }
//
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    [self tracking_dealloc];//最后调用
//}

- (NSString*)getControllerID{
    
//    return [TarckingDictionary getViewControllerIdentificationWithKey:NSStringFromClass(self.class)];
    return  @"";
}

@end
